<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" %>
 
 <script type="text/javascript" charset="UTF-8">
	var datagrid;
	$(function() {
		datagrid = $('#datagrid').datagrid({
			url : ctx+'/sys/user/list.htm',
			toolbar : '#userToolbar',
			title : '用户管理',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 10,
			pageList : [ 10, 20],
			fit : true,
			fitColumns : true,
			nowrap : false,
			border : false,
			idField : 'user_id',
			frozenColumns : [ [ {
				title : 'user_id',
				field : 'id',
				width : 50,
				checkbox : true
			}, {
				field : 'user_code',
				title : '帐号',
				width : 100,
				sortable : true
			}, {
				field : 'user_name',
				title : '名字',
				width : 100,
				sortable : true
			}] ],
			columns : [ [ {
				field : 'password',
				title : '密码',
				width : 100,
				formatter : function(value, rowData, rowIndex) {
					return '******';
				}
			},{
				field : 'email',
				title : '邮箱',
				width : 200
				 
			},{
				field : 'role_name',
				title : '角色',
				width : 100
				 
			},{
				field : 'state',
				title : '状态',
				width : 100,
				formatter : function(value, rowData, rowIndex) {
					if(value=="A")
						return '在用';
					else
						return '注销';
				}
			}, {
				field : 'ct_date',
				title : '创建时间',
				width : 150,
				sortable : true
			} ] ],
			onDblClickCell: function(index,field,value){
				var rows = datagrid.datagrid('getRows');
				openUserDialog(rows[index].user_id);
			}
		});
		
	});

	function openUserDialog(id) {
		var title="用户新增";
		var href=ctx+'/jsp/sys/user.jsp?id='+id;
		if(id.length>0){
			title="用户修改";
		}
		$('<div/>').dialog({
			href : href ,
			width : 520,
			height : 200,
			modal : true,
			title : title,
			buttons : [ {
				text : '保存',
				handler : function() {
					var d = $(this).closest('.window-body');
					var data=JSON.stringify($('#userForm').form("serialize"));
					$("#userForm input[name=data]").val(data);
					$('#userForm').form('submit', {
						url : ctx+'/sys/user/save.htm',
						success : function(result) {
							result = JSON.parse(result);
							$.messager.show({
								msg : result.r?"操作成功":"操作失败",
								title : "提示"
							});
							datagrid.datagrid('reload').datagrid('unselectAll');
							d.dialog('destroy');
						}
					});
				}
			},
			{
				text : '取消',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			}
			],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
	function userAppend(){
		openUserDialog("");
	}
	function userEdit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length != 1 && rows.length != 0) {
			var names = [];
			for ( var i = 0; i < rows.length; i++) {
				names.push(rows[i].name);
			}
			$.messager.show({
				msg : '只能择一个用户进行编辑！您已经选择了【' + names.join(',') + '】' + rows.length + '个用户',
				title : '提示'
			});
		} else if (rows.length == 1) {
			openUserDialog(rows[0].user_id);
		}else{
			$.messager.show({
				msg : '请选择一条记录进行操作',
				title : '提示'
			});
		}
	}
 
	function userRemove() {
		var ids = [];
		var rows = datagrid.datagrid('getSelections');
		if (rows.length > 0) {
			$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].user_id);
					}
					$.ajax({
						url : ctx+'/sys/user/del.htm',
						data : {
							id : ids.join(',')
						},
						cache : false,
						success : function(response) {
							datagrid.datagrid('unselectAll');
							datagrid.datagrid('reload');
							$.messager.show({
								title : '提示',
								msg : '删除成功！'
							});
						}
					});
				}
			});
		} else {
			$.messager.alert('提示', '请选择要删除的记录！', 'error');
		}
	}

	function userSearchFun() {
		datagrid.datagrid('load', {
			name : $('#userToolbar input[name=name]').val(),
			createdatetimeStart : $('#userToolbar input[comboname=createdatetimeStart]').datetimebox('getValue'),
			createdatetimeEnd : $('#userToolbar input[comboname=createdatetimeEnd]').datetimebox('getValue')
		});
	}
	function userClearFun() {
		$('#userToolbar input').val('');
		datagrid.datagrid('load', {});
	}
</script>
<div id="userToolbar" class="datagrid-toolbar" style="height: auto;">
	<fieldset>
		<legend>筛选</legend>
		<table class="tableForm">
			<tr>
				<th>名字</th>
				<td colspan="2"><input name="name" style="width: 305px;" /></td>
			</tr>
			<tr>
				<th>创建时间</th>
				<td ><input name="createdatetimeStart"  class="easyui-datebox"  editable="false" style="width: 150px;" />
					&nbsp;&nbsp;<input name="createdatetimeEnd" class="easyui-datebox"  editable="false" style="width: 150px;" /></td>
				<td>
				<a class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="userSearchFun();" href="javascript:void(0);">查找</a>
				<a class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="userClearFun();" href="javascript:void(0);">清空</a></td>
			</tr>
		 
		</table>
	</fieldset>
	<div>
		<a class="easyui-linkbutton" iconCls="icon-add" onclick="userAppend();" plain="true" href="javascript:void(0);">增加</a>
		<a class="easyui-linkbutton" iconCls="icon-remove" onclick="userRemove();" plain="true" href="javascript:void(0);">删除</a> 
		<a class="easyui-linkbutton" iconCls="icon-edit" onclick="userEdit();" plain="true" href="javascript:void(0);">编辑</a>
		<a class="easyui-linkbutton" iconCls="icon-undo" onclick="datagrid.datagrid('unselectAll');" plain="true" href="javascript:void(0);">取消选中</a>
	</div>
</div>
<table id="datagrid"></table>
  
 